home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 1.9 KB | 82 lines |
- package symantec.itools.awt.util;
-
-
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Toolkit;
- import java.awt.Image;
- import java.awt.Component;
-
-
- /**
- *
- *
- * @version 1.0, Nov 26, 1996
- *
- * @author Symantec
- *
- */
-
- // 04/11/97 LAB Added the getGraphics(Image image, Component component) function
- // 04/20/97 LAB Added import statements for Image and Component.
- // Fixed a type-o in the getGraphics function I added previously that
- // prevented it from working properly.
-
- public class Util
- {
- public static int getFontHeight(Graphics g)
- {
- return getFontHeight(g.getFontMetrics());
- }
-
- public static int getFontHeight(Font f)
- {
- return getFontHeight(Toolkit.getDefaultToolkit().getFontMetrics(f));
- }
-
- public static int getFontHeight(FontMetrics m)
- {
- return m.getHeight();
- }
-
- public static int getStringWidth(Graphics g, String s)
- {
- return getStringWidth(g.getFontMetrics(), s);
- }
-
- public static int getStringWidth(Font f, String s)
- {
- return getStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(f), s);
- }
-
- public static int getStringWidth(FontMetrics m, String s)
- {
- return m.stringWidth(s);
- }
-
- public static Font getDefaultFont()
- {
- return new Font("Dialog", 12, Font.PLAIN);
- }
-
- //getGraphics
- /**
- * Preserves the font information of a graphics object retrieved from
- * an image so that susequent calls to getFontMetrics through the graphics
- * object will not result in a NullPointerException.
- */
- public static Graphics getGraphics(Image image, Component component)
- {
- if(image == null)
- return null;
-
- Graphics graphics = image.getGraphics();
- if(graphics != null && component != null && component.getFont() != null)
- {
- graphics.setFont(component.getFont());
- }
- return graphics;
- }
- }
-